package com.sgg.demo25_redis;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
//注释2.。。。
//333333333333
//注释.....
// Generated by https://start.springboot.io
// 优质的 spring/boot/data/security/cloud 框架中文文档尽在 => https://springdoc.cn
@SpringBootApplication
public class Demo25RedisApplication implements InitializingBean {


    public static void main(String[] args) {
        SpringApplication.run(Demo25RedisApplication.class, args);
    }

    @Autowired
    RedisTemplate redisTemplate; //默认序列化器不好用，可以在项目启动后修改
    //当前类对象初始化之后 执行afterPropertiesSet ：spring生命周期方法
    @Override
    public void afterPropertiesSet() throws Exception {
        //配置键序列化器：  使用字符串的方式直接存储
        redisTemplate.setKeySerializer(StringRedisSerializer.UTF_8);
        redisTemplate.setHashKeySerializer(StringRedisSerializer.UTF_8);
        //配置 值的序列化器： 使用jackson将值转为 json字符串再存到redis中
        GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer();
        redisTemplate.setValueSerializer(serializer);
        redisTemplate.setHashValueSerializer(serializer);
    }
}
